﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace demo01_GDI基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_画直线_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Red, 5);
            Point p1 = new Point(10, 20);
            Point p2 = new Point(80, 95);
            g.DrawLine(p, p1, p2);

            g.Dispose();
            p.Dispose();
        }

        private void btn_清屏_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(BackColor);

            g.Dispose();
        }

        private void btn_画椭圆_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.CornflowerBlue);
            g.DrawEllipse(p, 120, 80, 300, 150);    //一个点(左上角),加上高度与宽度

            p.Dispose();
            g.Dispose();
        }

        private void btn_画圆_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;  //抗锯齿

            Pen p = new Pen(Color.Red);
            g.DrawEllipse(p, 120, 80, 200, 200);

            p.Dispose();
            g.Dispose();
        }

        private void btn_画矩形_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Green);
            g.DrawRectangle(p, 120, 80, 200, 150);
            p.Dispose();
            g.Dispose();
        }

        private void btn_画多边形_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Black);
            Point[] ps = new Point[4];
            ps[0].X = 120;
            ps[0].Y = 100;
            ps[1].X = 200;
            ps[1].Y = 100;
            ps[2].X = 200;
            ps[2].Y = 150;
            ps[3].X = 80;
            ps[3].Y = 180;
            g.DrawPolygon(p, ps);
            p.Dispose();
            g.Dispose();
        }

        private void btn_画圆弧_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Red, 20);
            Pen p1 = new Pen(Color.Green, 20);
            Pen p2 = new Pen(Color.Blue, 20);
            //DrawArc参数 : 画笔,矩形左上角x,矩形左上角y,矩形宽度,矩形高度,起始角度,绘制多少度(顺时针)
            g.DrawArc(p, 120, 100, 150, 150, 0, 120);
            g.DrawArc(p1, 120, 100, 150, 150, 120, 120);
            g.DrawArc(p2, 120, 100, 150, 150, 240, 120);
            g.Dispose();
            p.Dispose();
        }

        private void btn_画饼图_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Red, 2);
            g.DrawPie(p, 120, 100, 150, 150, 225, 90);
            g.FillPie(new SolidBrush(Color.Red), 280, 100, 150, 150, 225, 90);
            g.Dispose();
        }

        private void btn_曲线_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;  //抗锯齿

            Pen p = new Pen(Color.Red, 2);


            //起点坐标
            int beginX = 50;
            int beginY = 95;

            int height = 35;    //半波形默认高度
            int width = 50;     //半波形默认宽度

            //绘制坐标轴
            g.DrawLine(p, new Point(beginX, beginY), new Point(beginX + 210, beginY));
            g.DrawLine(p, new Point(beginX, beginY - 45), new Point(beginX, beginY + 45));


            //绘制曲线  DrawBezier参数(pen,开始点,第1控件点,第2控制点,结束点)
            g.DrawBezier(p, beginX, beginY, beginX + 15, beginY - height, beginX + 35, beginY - height, beginX + width, beginY);
            g.DrawBezier(p, beginX + width, beginY, beginX + width + 15, beginY + height, beginX + width + 35, beginY + height, beginX + width * 2, beginY);
            g.DrawBezier(p, beginX + width * 2, beginY, beginX + width * 2 + 15, beginY - height * 2, beginX + width * 2 + 35, beginY - height * 2, beginX + width * 3, beginY);
            g.DrawBezier(p, beginX + width * 3, beginY, beginX + width * 3 + 15, beginY + height * 2, beginX + width * 3 + 35, beginY + height * 2, beginX + width * 4, beginY);

            //绘制控制点
            int r = 3;
            Pen pen = new Pen(Color.Red);
            DrawCircle(g, pen, new Point(beginX + 15, beginY - height), r);
            DrawCircle(g, pen, new Point(beginX + 35, beginY - height), r);
            DrawCircle(g, pen, new Point(beginX + width + 15, beginY + height), r);
            DrawCircle(g, pen, new Point(beginX + width + 35, beginY + height), r);
            DrawCircle(g, pen, new Point(beginX + width * 2 + 15, beginY - height * 2), r);
            DrawCircle(g, pen, new Point(beginX + width * 2 + 35, beginY - height * 2), r);
            DrawCircle(g, pen, new Point(beginX + width * 3 + 15, beginY + height * 2), r);
            DrawCircle(g, pen, new Point(beginX + width * 3 + 35, beginY + height * 2), r);

            g.Dispose();
        }

        /// <summary>
        /// 圆心半径画
        /// </summary>
        /// <param name="g">Graphics</param>
        /// <param name="p">画刷</param>
        /// <param name="center">圆心</param>
        /// <param name="r">半径</param>
        void DrawCircle(Graphics g, Pen p, Point center, float r)
        {
            g.DrawEllipse(p, center.X - r, center.Y - r, 2 * r, 2 * r);
        }
    }
}
